﻿using B.S.DataBase.MyProject.Domain.BOM;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Read.API.Applicantions.Command.BomCOmmand;

namespace T.E.ERP.Read.API.Applicantions.CommandHandler.BomConnamdHandler
{
    /// <summary>
    /// 查询工艺流程
    /// 物联网-智能MES生产管理系统-01-BOM管理V1.1-20250206
    /// </summary>
    public class GetProcessInfoCommandHandler : IRequestHandler<GetProcessInfoCommand, APIResult<List<ProcessInfo>>>
    {
        private readonly IBaseRepository<ProcessInfo> proceRep;
        public GetProcessInfoCommandHandler(IBaseRepository<ProcessInfo> proceRep)
        {
            this.proceRep = proceRep;
        }

        public Task<APIResult<List<ProcessInfo>>> Handle(GetProcessInfoCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<ProcessInfo>> result = new APIResult<List<ProcessInfo>>();
            var list = proceRep.GetAll().Result;
            if (!string.IsNullOrEmpty(request.ProcessCode))
            {
                list = list.Where(x => x.ProcessCode == request.ProcessCode);
            }
            if (!string.IsNullOrEmpty(request.StepName))
            {
                list = list.Where(x => x.StepName.Contains(request.StepName));
            }

            list = list.OrderByDescending(x => x.ProcessId);
            result.Code = APIEnum.成功;
            result.Msg = APIEnum.查询成功.ToString();
            result.data = list.ToList();
            return Task.FromResult(result);
        }
    }
}
